package com.flyco.modules.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.common.api.vo.Result;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.modules.system.entity.SysPermissionResource;
import com.flyco.modules.system.entity.SysResource;
import com.flyco.modules.system.service.ISysPermissionResourceService;
import com.flyco.modules.system.service.ISysResourceService;
import com.flyco.modules.system.vo.MenuResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

 /**
 * @Description: sys_resource
 * @Author: flyco
 * @Date:   2022-12-07
 * @Version: V1.0
 */
@Slf4j
@Api(tags="sys_resource")
@RestController
@RequestMapping("/f/sys/sysResource")
public class SysResourceController extends BaseController<SysResource, ISysResourceService> {
	@Autowired
	private ISysResourceService sysResourceService;
	@Autowired
	private ISysPermissionResourceService sysPermissionResourceService;
	
	/**
	 * 分页列表查询
	 *
	 * @param sysResource
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "sys_resource-分页列表查询")
	@ApiOperation(value="sys_resource-分页列表查询", notes="sys_resource-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysResource sysResource,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysResource> queryWrapper = QueryGenerator.initQueryWrapper(sysResource, req.getParameterMap());
		Page<SysResource> page = new Page<SysResource>(pageNo, pageSize);
		IPage<SysResource> pageList = sysResourceService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param sysResource
	 * @return
	 */
	@AutoLog(value = "sys_resource-添加")
	@ApiOperation(value="sys_resource-添加", notes="sys_resource-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysResource sysResource) {
		sysResourceService.save(sysResource);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param sysResource
	 * @return
	 */
	@AutoLog(value = "sys_resource-编辑")
	@ApiOperation(value="sys_resource-编辑", notes="sys_resource-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysResource sysResource) {
		sysResourceService.updateById(sysResource);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "sys_resource-通过id删除")
	@ApiOperation(value="sys_resource-通过id删除", notes="sys_resource-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysResourceService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "sys_resource-批量删除")
	@ApiOperation(value="sys_resource-批量删除", notes="sys_resource-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysResourceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "sys_resource-通过id查询")
	@ApiOperation(value="sys_resource-通过id查询", notes="sys_resource-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysResource sysResource = sysResourceService.getById(id);
		return Result.ok(sysResource);
	}

	 @ApiOperation(value="查询菜单绑定资源", notes="查询菜单绑定资源")
	 @GetMapping(value = "/query-menu-bind")
	 public Result<?> queryMenuBind(@RequestParam(name="permissionId",required=true) String permissionId,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 Page<SysPermissionResource> page = new Page<>(pageNo, pageSize);
		 Page<SysPermissionResource> pageList = sysPermissionResourceService.page(page, Wrappers.lambdaQuery(SysPermissionResource.class).eq(SysPermissionResource::getPermissionId, permissionId));
		 if(pageList.getRecords()==null || pageList.getRecords().isEmpty()) {
			 return Result.ok(pageList);
		 }
		 List<SysResource> list = sysResourceService.list(Wrappers.lambdaQuery(SysResource.class).in(SysResource::getId, pageList.getRecords().stream().map(SysPermissionResource::getResourceId).collect(Collectors.toList())));
		 Page p = new Page();
		 BeanUtil.copyProperties(pageList,p);
		 p.setRecords(list);
		 return Result.ok(p);
	 }

	 @ApiOperation(value="菜单解绑资源", notes="菜单解绑资源")
	 @PostMapping(value = "/menu-un-bind")
	 public Result<?> menuUnBind(@RequestBody MenuResource menuResource) {
		 if(StrUtil.isBlank(menuResource.getPermissionId()) || StrUtil.isBlank(menuResource.getResourceIds())){
			 return Result.error("参数不能为空");
		 }
		 List<Long> resourceIds = Arrays.stream(menuResource.getResourceIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
		 sysPermissionResourceService.remove(Wrappers.lambdaQuery(SysPermissionResource.class)
				 .eq(SysPermissionResource::getPermissionId,menuResource.getPermissionId())
				 .in(SysPermissionResource::getResourceId,resourceIds));
		 return Result.ok("解绑成功");
	 }

	 @ApiOperation(value="菜单绑定资源", notes="菜单绑定资源")
	 @PostMapping(value = "/menu-bind")
	 public Result<?> menuBind(@RequestBody MenuResource menuResource) {
		if(StrUtil.isBlank(menuResource.getPermissionId()) || StrUtil.isBlank(menuResource.getResourceIds())){
			return Result.error("参数不能为空");
		}

		 List<Long> resourceIds = Arrays.stream(menuResource.getResourceIds().split(",")).map(Long::parseLong).collect(Collectors.toList());

		 //查询之前的记录
		 List<Long> list = sysPermissionResourceService.list(Wrappers.lambdaQuery(SysPermissionResource.class).eq(SysPermissionResource::getPermissionId, menuResource.getPermissionId()))
				 .stream().map(SysPermissionResource::getResourceId).collect(Collectors.toList());
		 if(!list.isEmpty()){
		 	//排除已经存在的记录
			resourceIds.removeIf(list::contains);
		}

		if(resourceIds.isEmpty()){
			return Result.error("没有要保存的新数据");
		}

		 //补充本次新增记录
		 List<SysPermissionResource> collect = resourceIds.stream().map(resourceId -> {
			 SysPermissionResource sysPermissionResource = new SysPermissionResource();
			 sysPermissionResource.setPermissionId(menuResource.getPermissionId());
			 sysPermissionResource.setResourceId(resourceId);
			 return sysPermissionResource;
		 }).collect(Collectors.toList());
		 sysPermissionResourceService.saveBatch(collect);

		 return Result.ok("添加成功！");
	 }

}
